package pub.doric.extension;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

public class SkiaView extends View {

    static {
        System.loadLibrary("skia");
        System.loadLibrary("native-lib");
    }

    private Bitmap mBitmap;
    private Paint mPaint = new Paint();

    public SkiaView(Context context) {
        super(context);
    }

    public SkiaView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public SkiaView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (mBitmap == null) {
            mBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        }

        nativeRender(mBitmap);
        canvas.drawBitmap(mBitmap, 0, 0, mPaint);
    }

    private native void nativeRender(Bitmap bitmap);
}
